const utils = {
    isNullOrEmpty: function (str) {
        if (typeof (str) === 'string') {
            if (str.trim() == '') {
                return true;
            } else {
                return false;
            }
        }
        return true;
    },
    isNumber(str) {
        return /^\d+(\.\d+)?$/.test(str);
    },
    parseBoolean: function (str) {
        if (typeof (str) !== 'string') {
            return false;
        }
        if (str === 'false' || str === '0') {
            return false;
        } else if (str === 'true' || str === '1') {
            return true;
        }
    },
    isEmail(str) {
        return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(str.toString());
    },
    parseFloat: function (str) {
        let result = Number(str);
        if (!isNaN(result)) {
            return Number(result.toFixed(2));
        } else {
            return 0.00;
        }
    },
    getTimeStamp: function () {
        return (new Date()).valueOf();
    },
    formatDateTime: function (inputTime) {
        if (inputTime <= 0 || inputTime == undefined) {
            return '';
        }
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let mStr = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        let dStr = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        let hStr = h < 10 ? ('0' + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        let minuteStr = minute < 10 ? ('0' + minute) : minute;
        let secondStr = second < 10 ? ('0' + second) : second;
        return `${y}-${mStr}-${dStr} ${hStr}:${minuteStr}:${secondStr}`;
    },
    formatDuring: function (mss) {
        var days = parseInt((mss / (1000 * 60 * 60 * 24)).toString());
        var hours = parseInt(((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toString());
        var minutes = parseInt(((mss % (1000 * 60 * 60)) / (1000 * 60)).toString());
        var seconds = (mss % (1000 * 60)) / 1000;
        return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
    },
    getClientIP: function (req) {
        var ipAddress;
        var forwardedIpsStr = req.header('x-forwarded-for');
        if (forwardedIpsStr) {
            var forwardedIps = forwardedIpsStr.split(',');
            ipAddress = forwardedIps[0];
        }
        if (!ipAddress) {
            ipAddress = req.connection.remoteAddress;
        }
        return ipAddress;
    }
}
export default utils